package com.spring.mvc.http.jcex;

public class CryptorXMD5 {
	static final int S11 = 7;
	static final int S12 = 12;
	static final int S13 = 17;
	static final int S14 = 22;
	static final int S21 = 5;
	static final int S22 = 9;
	static final int S23 = 14;
	static final int S24 = 20;
	static final int S31 = 4;
	static final int S32 = 11;
	static final int S33 = 16;
	static final int S34 = 23;
	static final int S41 = 6;
	static final int S42 = 10;
	static final int S43 = 15;
	static final int S44 = 21;
	static final byte[] PADDING = { -128 };
	static final char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
			'9', 'A', 'B', 'C', 'D', 'E', 'F' };
	private long[] state;
	private long[] count;
	private byte[] buffer;
	private byte[] digest;

	public static String byteHEX(byte ib) {
		return new String(new char[] { Digit[(ib >>> 4 & 0xF)],
				Digit[(ib & 0xF)] });
	}

	private static final long b2iu(byte b) {
		return ((b >= 0) ? b : b & 0xFF);
	}

	public CryptorXMD5() {
		this.state = new long[4];
		this.count = new long[2];
		this.buffer = new byte[64];
		this.digest = new byte[16];
	}

	public String digest(String inbuf) {
		md5Init();
		md5Update(inbuf.getBytes(), inbuf.length());
		md5Final();
		StringBuffer buffer = new StringBuffer();
		for (int i = 0; i < 16; ++i)
			buffer.append(byteHEX(this.digest[i]));

		return buffer.toString();
	}

	public byte[] digest(byte[] inbuf) {
		md5Init();
		md5Update(inbuf, inbuf.length);
		md5Final();

		byte[] bytes = new byte[16];

		System.arraycopy(this.digest, 0, bytes, 0, bytes.length);

		return bytes;
	}

	private final long F(long x, long y, long z) {
		return (x & y | (x ^ 0xFFFFFFFF) & z);
	}

	private final long FF(long a, long b, long c, long d, long x, long s,
			long ac) {
		a += F(b, c, d) + x + ac;
		a = (int) a << (int) s | (int) a >>> (int) (32L - s);
		return (a + b);
	}

	private final long G(long x, long y, long z) {
		return (x & z | y & (z ^ 0xFFFFFFFF));
	}

	private final long GG(long a, long b, long c, long d, long x, long s,
			long ac) {
		a += G(b, c, d) + x + ac;
		a = (int) a << (int) s | (int) a >>> (int) (32L - s);
		return (a + b);
	}

	private final long H(long x, long y, long z) {
		return (x ^ y ^ z);
	}

	private final long HH(long a, long b, long c, long d, long x, long s,
			long ac) {
		a += H(b, c, d) + x + ac;
		a = (int) a << (int) s | (int) a >>> (int) (32L - s);
		return (a + b);
	}

	private final long I(long x, long y, long z) {
		return (y ^ (x | z ^ 0xFFFFFFFF));
	}

	private final long II(long a, long b, long c, long d, long x, long s,
			long ac) {
		a += I(b, c, d) + x + ac;
		a = (int) a << (int) s | (int) a >>> (int) (32L - s);
		return (a + b);
	}

	private void md5decode(byte[] input, long[] output, int len) {
		int i = 0;
		for (int j = 0; j < len; j += 4) {
			output[i] = (b2iu(input[j]) | b2iu(input[(j + 1)]) << 8
					| b2iu(input[(j + 2)]) << 16 | b2iu(input[(j + 3)]) << 24);
			++i;
		}
	}

	private void md5encode(byte[] output, long[] input, int len) {
		int i = 0;
		for (int j = 0; j < len; j += 4) {
			output[j] = (byte) (int) (input[i] & 0xFF);
			output[(j + 1)] = (byte) (int) (input[i] >>> 8 & 0xFF);
			output[(j + 2)] = (byte) (int) (input[i] >>> 16 & 0xFF);
			output[(j + 3)] = (byte) (int) (input[i] >>> 24 & 0xFF);
			++i;
		}
	}

	private void md5Final() {
		byte[] bits = new byte[8];
		md5encode(bits, this.count, 8);
		int index = (int) (this.count[0] >>> 3) & 0x3F;
		int padLen = (index >= 56) ? 120 - index : 56 - index;
		md5Update(PADDING, padLen);
		md5Update(bits, 8);
		md5encode(this.digest, this.state, 16);
	}

	private void md5Init() {
		this.count[0] = 0L;
		this.count[1] = 0L;
		this.state[0] = 1732584193L;
		this.state[1] = -271733879L;
		this.state[2] = -1732584194L;
		this.state[3] = 271733878L;
	}

	private void md5Memcpy(byte[] output, byte[] input, int outpos, int inpos,
			int len) {
		System.arraycopy(input, inpos, output, outpos, len);
	}

	private void md5Transform(byte[] block) {
		long a = this.state[0];
		long b = this.state[1];
		long c = this.state[2];
		long d = this.state[3];
		long[] x = new long[16];
		md5decode(block, x, 64);
		a = FF(a, b, c, d, x[0], 7L, -680876936L);
		d = FF(d, a, b, c, x[1], 12L, -389564586L);
		c = FF(c, d, a, b, x[2], 17L, 606105819L);
		b = FF(b, c, d, a, x[3], 22L, -1044525330L);
		a = FF(a, b, c, d, x[4], 7L, -176418897L);
		d = FF(d, a, b, c, x[5], 12L, 1200080426L);
		c = FF(c, d, a, b, x[6], 17L, -1473231341L);
		b = FF(b, c, d, a, x[7], 22L, -45705983L);
		a = FF(a, b, c, d, x[8], 7L, 1770035416L);
		d = FF(d, a, b, c, x[9], 12L, -1958414417L);
		c = FF(c, d, a, b, x[10], 17L, -42063L);
		b = FF(b, c, d, a, x[11], 22L, -1990404162L);
		a = FF(a, b, c, d, x[12], 7L, 1804603682L);
		d = FF(d, a, b, c, x[13], 12L, -40341101L);
		c = FF(c, d, a, b, x[14], 17L, -1502002290L);
		b = FF(b, c, d, a, x[15], 22L, 1236535329L);
		a = GG(a, b, c, d, x[1], 5L, -165796510L);
		d = GG(d, a, b, c, x[6], 9L, -1069501632L);
		c = GG(c, d, a, b, x[11], 14L, 643717713L);
		b = GG(b, c, d, a, x[0], 20L, -373897302L);
		a = GG(a, b, c, d, x[5], 5L, -701558691L);
		d = GG(d, a, b, c, x[10], 9L, 38016083L);
		c = GG(c, d, a, b, x[15], 14L, -660478335L);
		b = GG(b, c, d, a, x[4], 20L, -405537848L);
		a = GG(a, b, c, d, x[9], 5L, 568446438L);
		d = GG(d, a, b, c, x[14], 9L, -1019803690L);
		c = GG(c, d, a, b, x[3], 14L, -187363961L);
		b = GG(b, c, d, a, x[8], 20L, 1163531501L);
		a = GG(a, b, c, d, x[13], 5L, -1444681467L);
		d = GG(d, a, b, c, x[2], 9L, -51403784L);
		c = GG(c, d, a, b, x[7], 14L, 1735328473L);
		b = GG(b, c, d, a, x[12], 20L, -1926607734L);
		a = HH(a, b, c, d, x[5], 4L, -378558L);
		d = HH(d, a, b, c, x[8], 11L, -2022574463L);
		c = HH(c, d, a, b, x[11], 16L, 1839030562L);
		b = HH(b, c, d, a, x[14], 23L, -35309556L);
		a = HH(a, b, c, d, x[1], 4L, -1530992060L);
		d = HH(d, a, b, c, x[4], 11L, 1272893353L);
		c = HH(c, d, a, b, x[7], 16L, -155497632L);
		b = HH(b, c, d, a, x[10], 23L, -1094730640L);
		a = HH(a, b, c, d, x[13], 4L, 681279174L);
		d = HH(d, a, b, c, x[0], 11L, -358537222L);
		c = HH(c, d, a, b, x[3], 16L, -722521979L);
		b = HH(b, c, d, a, x[6], 23L, 76029189L);
		a = HH(a, b, c, d, x[9], 4L, -640364487L);
		d = HH(d, a, b, c, x[12], 11L, -421815835L);
		c = HH(c, d, a, b, x[15], 16L, 530742520L);
		b = HH(b, c, d, a, x[2], 23L, -995338651L);
		a = II(a, b, c, d, x[0], 6L, -198630844L);
		d = II(d, a, b, c, x[7], 10L, 1126891415L);
		c = II(c, d, a, b, x[14], 15L, -1416354905L);
		b = II(b, c, d, a, x[5], 21L, -57434055L);
		a = II(a, b, c, d, x[12], 6L, 1700485571L);
		d = II(d, a, b, c, x[3], 10L, -1894986606L);
		c = II(c, d, a, b, x[10], 15L, -1051523L);
		b = II(b, c, d, a, x[1], 21L, -2054922799L);
		a = II(a, b, c, d, x[8], 6L, 1873313359L);
		d = II(d, a, b, c, x[15], 10L, -30611744L);
		c = II(c, d, a, b, x[6], 15L, -1560198380L);
		b = II(b, c, d, a, x[13], 21L, 1309151649L);
		a = II(a, b, c, d, x[4], 6L, -145523070L);
		d = II(d, a, b, c, x[11], 10L, -1120210379L);
		c = II(c, d, a, b, x[2], 15L, 718787259L);
		b = II(b, c, d, a, x[9], 21L, -343485551L);
		this.state[0] += a;
		this.state[1] += b;
		this.state[2] += c;
		this.state[3] += d;
	}

	private void md5Update(byte[] inbuf, int inputLen) {
		int i;
		byte[] block = new byte[64];
		int index = (int) (this.count[0] >>> 3) & 0x3F;
		if ((this.count[0] += (inputLen << 3)) < inputLen << 3)
			this.count[1] += 1L;
		this.count[1] += (inputLen >>> 29);
		int partLen = 64 - index;

		if (inputLen >= partLen) {
			md5Memcpy(this.buffer, inbuf, index, 0, partLen);
			md5Transform(this.buffer);
			for (i = partLen; i + 63 < inputLen; i += 64) {
				md5Memcpy(block, inbuf, 0, i, 64);
				md5Transform(block);
			}

			index = 0;
		} else {
			i = 0;
		}
		md5Memcpy(this.buffer, inbuf, index, i, inputLen - i);
	}
}